package com.javasvip.serviceemail.controller;


import com.javasvip.common.pojo.AuthCode;
import com.javasvip.serviceemail.service.AuthCodeService;
import com.javasvip.serviceemail.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;

@RestController
@RequestMapping("/mail")
public class mailController {
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    AuthCodeService authCodeService;
    @Autowired
    MailService mailService;


    /**
     * 发送邮件
     *
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/sendMail/{email}/{code}")
    public Integer sendMail(@PathVariable String email, @PathVariable String code) {
        System.out.println("发送邮件=====" + email + "==" + code);
        /*  mailService.sendMail("email","您的验证码是："+code);*/
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("jr-zywqd01@jybdshop.cn");
        message.setTo(email);
        message.setSubject("主题：");
        message.setText("测试" + code);

        mailSender.send(message);
        return 1;
    }


    @GetMapping("/main/{userId}")
    public Integer main(@PathVariable Long userId) {


        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("jr-zywqd01@jybdshop.cn");
        message.setTo("903950191@qq.com");
        message.setSubject("主题：简单邮件");
        message.setText("测试邮件内容");

        mailSender.send(message);
        return 1;
    }

    @GetMapping("/all/{userId}")
    public Integer all(@PathVariable Long userId) {
        int newNum = (int) ((Math.random() * 9 + 1) * 100000);//随机码
        AuthCode authCode = new AuthCode();
        authCode.setEmail("");
        authCode.setCode(newNum + "");
        Timestamp createTime = new Timestamp(new Date().getTime());
        authCode.setCreatetime(createTime);
        Timestamp expireTime = new Timestamp(new Date().getTime() + (1000 * 60 * 10));
        authCode.setExpiretime(expireTime);
        authCodeService.save(authCode);


        return 1;
    }

    public static void main(String[] args) {
        System.out.println(Math.random());
        int newNum = (int) ((Math.random() * 9 + 1) * 100000);
        System.out.println(newNum);


        Timestamp expireTime = new Timestamp(new Date().getTime() + (1000 * 60 * 10));
        System.out.println(expireTime);
    }


}
